/**
 * 存放经常要使用的信息在这个对象里
 * 减少查询数据库的次数
 */
package cn.antia.cep.common.pool;

import java.util.*;

import cn.antia.cep.busi.*;
import cn.antia.cep.common.*;
import cn.antia.cwin.DBConfiger;
import cn.antia.cwin.Cele;
import cn.antia.cwin.grid.GridConfiger;
import cn.antia.cwin.grid.SingleGrid;
import cn.antia.cwin.window.MultiWindow;
import cn.antia.cwin.window.SingleWindow;
import cn.antia.cwin.window.WindowConfiger;

/**
 * @author sunkeei
 * 
 */
public class AntiaPool {
	/**
	 * 数据服务类
	 */
	private DbHibernateService dbHibernateService;
	public DbHibernateService getDbHibernateService() {
		return dbHibernateService;
	}
	public void setDbHibernateService(DbHibernateService dbHibernateService) {
		this.dbHibernateService = dbHibernateService;
	}

	/**
	 * 存储的信息： t_表名:表结构--List:ElementComsat对象
	 * 
	 */
    private Map<String, Cele> commonElements;       //公用的FIELD定义
    private HashMap dbConfigElements;		//数据表配置
	private HashMap gridElements;			//表格配置元素
	private HashMap singleWindows;				//window config
	private HashMap multiWindows;			//multiWindows
	private List resources;					// 所有的资源列表
	
	public HashMap getDbConfigElements() {
		return dbConfigElements;
	}
	public void setDbConfigElements(HashMap dbConfigElements) {
		this.dbConfigElements = dbConfigElements;
	}
	public HashMap getGridElements() {
		return gridElements;
	}
	public void setGridElements(HashMap gridElements) {
		this.gridElements = gridElements;
	}
	public List getResources() {
		return resources;
	}
	public void setResources(List resources) {
		this.resources = resources;
	}
    public Map<String, Cele> getCommonElements() {
        return commonElements;
    }

    public void setCommonElements(Map<String, Cele> commonElements) {
        this.commonElements = commonElements;
    }

    public List getExceptionInfo() {
		return null;
	}

	/**
	 * 取得通信对象集合
	 * 
	 * @param table
	 *            表名
	 * @return 通信对象列表
	 * @throws cn.antia.cep.common.AntiaException
	 *             a
	 */
	public List getElementComsats(String table) throws AntiaException {
		String key = "t_" + table;
		ArrayList comsats;
		List relist;
		if (dbConfigElements == null) {
			dbConfigElements = new HashMap();
			comsats = (ArrayList) (DBConfiger.getTableElements(table));
			dbConfigElements.put(key, comsats);
			relist = (List) comsats.clone();
		} else {
			comsats = (ArrayList) dbConfigElements.get(key);
			if (comsats == null || comsats.size() < 1) {
				comsats = (ArrayList) (DBConfiger.getTableElements(table));
				dbConfigElements.put(key, comsats);
			}
			relist = (List) comsats.clone();
		}
		return relist;
	}

	public void resetAllComsats() {
		dbConfigElements = null;
	}

	/**
	 * 取得所有资源
	 */
	public List getAllResources() throws AntiaException {
		if (resources == null) {
			reObtainResources();
		}
		return resources;
	}
	
	/**
	 * 初始化资源列表
	 */
	public void reObtainResources() throws AntiaException {
		String hql = "from Resource where valid = 1";
		resources = dbHibernateService.find(hql);
		if (resources == null || resources.size() < 1) {
			throw new AntiaException("common003", "", false);
		}
	}
	/**
	 * 取得单表表格配置
	 * @param gridId
	 * @return
	 * @throws AntiaException
	 */
	public SingleGrid getSingleGrid(String gridId)throws AntiaException{
		if(gridElements == null){
			gridElements = new HashMap();
		}
		String key = AntiaPoolConst.SINGLE_GRID_PREFIX + gridId;
		SingleGrid grid = (SingleGrid)gridElements.get(key);
		if(grid == null){
			grid = GridConfiger.getSingleGrid(gridId);
			gridElements.put(key, grid);
		}
		return grid;
	}
	/**
	 * 取得配置的操作窗口
	 * @param winId window id
	 * @return SingleWindow
	 * @throws AntiaException
	 */
	public SingleWindow getSingleWindow(String winId)throws AntiaException{
		if(singleWindows == null){
			singleWindows = new HashMap();
		}
		SingleWindow window = (SingleWindow)singleWindows.get(winId);
		if(window == null){
			window = WindowConfiger.getSingleWindow(winId);;
			singleWindows.put(winId, window);
		}
		return window;
	}
	/**
	 * 取得配置的操作窗口(多窗口)
	 * @param winId
	 * @return
	 * @throws AntiaException
	 */
	public MultiWindow getMultiWindow(String winId)throws AntiaException{
		if(multiWindows == null){
			multiWindows = new HashMap();
		}
		MultiWindow window = (MultiWindow)multiWindows.get(winId);
		if(window == null){
			window = WindowConfiger.getMultiWindow(winId);
			multiWindows.put(winId, window);
		}
		return window;
	}
	
	
	
	public HashMap getSingleWindows() {
		return singleWindows;
	}
	public void setSingleWindows(HashMap singleWindows) {
		this.singleWindows = singleWindows;
	}
	
	
}